package com.sls.web.manager.game.service.impl;

import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.SnnsGoodsInfo;
import com.sls.web.manager.game.querybean.SnnsProductQuery;
import com.sls.web.manager.game.service.SnnsGoodsService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

/**
 * 
 * @description 7996商品管理
 * @author Wangxuegang
 * @date 2013-4-23
 */
@Repository("snnsGoodsService")
public class SnnsGoodsServiceImpl extends SuperService implements
		SnnsGoodsService {

	private Gson gson;

	public SnnsGoodsServiceImpl() {
		gson = CommonUtil.gson();
	}

	/**
	 * 批量上架
	 * 
	 * @param goodsState
	 * @param userInfoVO
	 */
	@Override
	public void batchOnline(String goodsState, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_SUPPLY_UPDATE_STATE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		SnnsGoodsInfo snnsGoodsInfo = new SnnsGoodsInfo();
		snnsGoodsInfo.setGoodsState(goodsState);
		snnsGoodsInfo.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, snnsGoodsInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 批量下架
	 * 
	 * @param goodsState
	 * @param userInfoVO
	 */
	@Override
	public void batchOffline(String goodsState, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_SUPPLY_UPDATE_STATE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		SnnsGoodsInfo snnsGoodsInfo = new SnnsGoodsInfo();
		snnsGoodsInfo.setGoodsState(goodsState);
		snnsGoodsInfo.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, snnsGoodsInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 商品批量审核
	 * 
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean confirmAudit(String suppliersId, int priceRatio,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SNNS_GOODS_AUDIT);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		SnnsGoodsInfo snnsGoodsInfo = new SnnsGoodsInfo();
		snnsGoodsInfo.setSuppliersId(suppliersId);
		snnsGoodsInfo.setPriceRatio(priceRatio);
		snnsGoodsInfo.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, snnsGoodsInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 数据保存
	 * 
	 * @param snnsGoodsInfo
	 * @param userInfoVO
	 * @throws Exception
	 */
	@Override
	public void save(SnnsGoodsInfo snnsGoodsInfo, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SNNS_GOODS_SAVE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		SocketDTO dto = super.send(head, snnsGoodsInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 分页查询
	 * 
	 * @param enlinkQuery
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public Pagination<SnnsGoodsInfo> querySnnsProduct(
			SnnsProductQuery snnsQuery, UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SNNS_GOODS_PAGE_INQUIRE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, snnsQuery);

		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<SnnsGoodsInfo> page = gson.fromJson(body,
					new TypeToken<Pagination<SnnsGoodsInfo>>() {
					}.getType());
			return page;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 根据商品id,查询商品记录
	 * 
	 * @param product_id
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public SnnsGoodsInfo queryByProductId(String product_id,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SNNS_GOODS_QUERY_BY_GOODSID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		SnnsGoodsInfo snnsGoodsInfo = new SnnsGoodsInfo();
		snnsGoodsInfo.setProduct_id(product_id);
		SocketDTO dto = super.send(head, snnsGoodsInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				SnnsGoodsInfo sgInfo = gson.fromJson(body, SnnsGoodsInfo.class);
				return sgInfo;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 记录更新
	 * 
	 * @param snnsGoodsInfo
	 * @param userInfoVO
	 * @throws Exception
	 */
	@Override
	public void update(SnnsGoodsInfo snnsGoodsInfo, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SNNS_GOODS_UPDATE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, snnsGoodsInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

}
